package com.tanhua.server.service;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.RecommendUserQueryParam;
import com.tanhua.domain.vo.TodayBestVo;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TodayBestService {

    @Reference
    private RecommendUserApi recommendUserApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private QuestionApi questionApi;

    /**
     * 查询今日佳人
     */
    public ResponseEntity queryTodayBest() {
        //1、获取当前登录用户的用户id
        Long userId = UserHolder.getUserId();
        //2、调用api查询数据
        RecommendUser user = recommendUserApi.queryWithMaxScore(userId);
        if (user == null) {
            user = new RecommendUser();  //当推荐用户不存在时，默认值
            user.setUserId(2l);
            user.setScore(95d);
        }
        //3、根据查询的推荐用户，查询用户信息
        UserInfo info = userInfoApi.findByUserId(user.getUserId());
        //4、构造vo对象
        TodayBestVo best = new TodayBestVo();
        BeanUtils.copyProperties(info, best);
        best.setFateValue(user.getScore().longValue());
        if (!StringUtils.isEmpty(info.getTags())) {
            best.setTags(info.getTags().split(","));
        }
        //5、返回值
        return ResponseEntity.ok(best);
    }


    //推荐好友列表
    public PageResult<TodayBestVo> recommendList(RecommendUserQueryParam queryParam) {
        //根据token获取当前的用户信息
        Long userId = UserHolder.getUserId();
        PageResult result = recommendUserApi.findPage(queryParam.getPage(), queryParam.getPagesize(), userId);
        List<RecommendUser> records = (List<RecommendUser>) result.getItems();
        //进行判断，如果没查到，使用默认推荐
        if (CollectionUtils.isEmpty(records)) {
            result = new PageResult(10l, queryParam.getPagesize().longValue(), 1l, 1l, null);
            records=defaultRecommend();
        }
        List<TodayBestVo> todayBests=new ArrayList<>();
        for (RecommendUser record : records) {
            TodayBestVo bestVo=new TodayBestVo();
            //补全用户信息
            UserInfo userInfo=this.userInfoApi.findByUserId(record.getUserId());
            BeanUtils.copyProperties(userInfo,bestVo);
            bestVo.setFateValue(record.getScore().longValue());
            bestVo.setTags(StringUtils.split(userInfo.getTags(),','));
            todayBests.add(bestVo);
        }
        //返回vo
        result.setItems(todayBests);
        return result;
    }

    //构造默认数据
    private List<RecommendUser> defaultRecommend() {
        String ids = "1,2,3,4,5,6,7,8,9,10";
        List<RecommendUser> records = new ArrayList<>();
        for (String id : ids.split(",")) {
            RecommendUser recommendUser = new RecommendUser();
            recommendUser.setUserId(Long.valueOf(id));
            recommendUser.setScore(RandomUtils.nextDouble(70, 98));
            records.add(recommendUser);
        }
        return records;
    }


    //佳人信息
    public TodayBestVo getUserInfo(Long userId) {
        //获取用户详情
        UserInfo userInfo=userInfoApi.findByUserId(userId);
        //创建vo
        TodayBestVo bestVo=new TodayBestVo();
        //复制属性
        BeanUtils.copyProperties(userInfo,bestVo,"tags");
        //查询推荐佳人的缘分值
        Double score=recommendUserApi.queryForScore(userId,UserHolder.getUserId());
        if(score == null){
            bestVo.setFateValue(80l);
        }else {
            bestVo.setFateValue(score.longValue());
        }
        bestVo.setTags(StringUtils.split(userInfo.getTags(),","));
        return bestVo;

    }


    //查询用户的陌生人问题
    public String queryStrangerQuestions(Long userId) {
      Question question= questionApi.findByUserId(userId);
      if(null==question||StringUtils.isEmpty(question.getTxt())){
          return "你喜欢我吗？";
      }
        return question.getTxt();
    }
}